<?php
/*由bean脚本生成 powerBy--joe*/
namespace app\common\manage;
use app\common\bean\UserBean;
use app\common\enum\CategoryGroupTypeEnum;
use app\common\enum\CategoryTypeEnum;
use app\common\enum\ContentTypeEnum;
use app\common\model\UserModel;
use app\common\utils\ExitJsonUtil;
use app\common\validate\ContentValidate;
use app\common\model\ContentModel;
use app\common\bean\ContentBean;
use app\common\bean\ListMap;
use app\common\utils\TimeUtil;
use app\common\exception\MyException;
use Exception;
class ContentManage extends BaseManage
{
	/**
	* @return ContentManage
	*/
	static function getInstance(){
		return parent::getInstance();
	}
    public function getService(ListMap $listMap){
	    $user = UserModel::getInstance()->selectData([UserBean::$id=>$listMap->getParameter('uid')]);
        return ContentModel::getInstance()->selectData([ContentBean::$storeId=>$user[UserBean::$storeId]], true);
    }
	/**
	 * @desc 获取分页数据
	 * @param ListMap $listMap
	 * @return ListMap $listMap
	 * @throws Exception
	**/
	public function getPaginatorList(ListMap $listMap) {
		$conditionMap = new ListMap();
		if($timeArray = TimeUtil::getInstance()->dealBetweenTime($listMap))
		$conditionMap->setParameter(ContentBean::$alias.POINST.ContentBean::$createdAt,$timeArray);
		$conditionMap->setAlias(ContentBean::$alias);
		$listMap->setField(ContentBean::$alias.ALL_FIELD);
		!$this->getParamBool($listMap,"orderFieldName") ?: $conditionMap->setOrder($listMap->getParameter('orderFieldName').SPACE.$listMap->getParameter('orderType'));
		$conditionMap->setParameter(ContentBean::$storeId,$listMap->getParameter(ContentBean::$storeId));
		$resultMap = ContentModel::getInstance()->getList($conditionMap);
		$resultMap?:MyException::throwMyException("查询数据失败");
		return $resultMap;
	}
	
	/**
	 * @desc 添加数据
	 * @param ContentBean $contentBean
	 * @return int int
	 * @throws Exception
	**/
	public function add($contentBean) {
		return null;
	}
	
	/**
	 * @desc 编辑数据
	 * @param ContentBean $contentBean
	 * @return int int
	 * @throws Exception
	**/
	public function edit($contentBean) {
		ContentValidate::getInstance()->goCheck(ContentValidate::$edit,$contentBean->getData());
		try{
            $result = ContentModel::getInstance()->updatedData([ContentBean::$id => $contentBean->getId(),ContentBean::$storeId => $contentBean->getStoreId()],$contentBean->getData());
            $result !== false ?: MyException::throwMyException("更新失败");
        }catch (Exception $exception){
            ContentModel::getInstance()->rollback();
            MyException::throwMyException($exception->getMessage());
        }
		return $result;
	}
	
	/**
	 * @desc 删除数据
	 * @param ContentBean $contentBean
	 * @return int int
	 * @throws Exception
	**/
	public function delete($contentBean) {
		return null;
	}
	
	/**
	 * @desc 根据主建获取数据
	 * @param ContentBean $contentBean
	 * @return ContentBean $contentBean
	 * @throws Exception
	**/
	public function getInfo($contentBean) {
		ContentValidate::getInstance()->goCheck(ContentValidate::$pkId,$contentBean->getData());
		// Todo:添加业务处理代码...
		$result = ContentModel::getInstance()->selectData([ContentBean::$id => $contentBean->getId()]);
		if($result[ContentBean::$storeId]==0){
		    //ExitJsonUtil::getInstance()->exitData($result, 501, '不允许默认数据');
        }
		$result ?: MyException::throwMyException("获取信息失败");
        $contentBean->setData($result);
        $contentBean->setParameter('title',ContentTypeEnum::getName($contentBean->getType()));
        $categoryManage = CategoryManage::getInstance();
		switch($contentBean->getType()){
            case ContentTypeEnum::DEVPHOTO:
                $specs = [
                    ['type'=>CategoryTypeEnum::DEVPHOTOXZ,'name'=>CategoryTypeEnum::getName(CategoryTypeEnum::DEVPHOTOXZ),'list'=>$categoryManage->getDealData($contentBean->getStoreId(),CategoryTypeEnum::DEVPHOTOXZ)],
                    ['type'=>CategoryTypeEnum::DEVPHOTOCC,'name'=>CategoryTypeEnum::getName(CategoryTypeEnum::DEVPHOTOCC),'list'=>$categoryManage->getDealData($contentBean->getStoreId(),CategoryTypeEnum::DEVPHOTOCC)]
                ];
                break;
            case ContentTypeEnum::CERPHOTO:
                $specs = [
                    ['type'=>CategoryTypeEnum::CERPHOTOLX,'name'=>CategoryTypeEnum::getName(CategoryTypeEnum::CERPHOTOLX),'list'=>$categoryManage->getDealData($contentBean->getStoreId(),CategoryTypeEnum::CERPHOTOLX)]
                ];
                break;
            case ContentTypeEnum::PRIPHOTO:
                $specs = [
                    ['type'=>CategoryTypeEnum::PRIPHOTOSC,'name'=>CategoryTypeEnum::getName(CategoryTypeEnum::PRIPHOTOSC),'list'=>$categoryManage->getDealData($contentBean->getStoreId(),CategoryTypeEnum::PRIPHOTOSC)],
                    ['type'=>CategoryTypeEnum::PRIPHOTODX,'name'=>CategoryTypeEnum::getName(CategoryTypeEnum::PRIPHOTODX),'list'=>$categoryManage->getDealData($contentBean->getStoreId(),CategoryTypeEnum::PRIPHOTODX)]
                ];
                break;
            default:
                MyException::throwMyException("类型出错");
        };
        $contentBean->setParameter('specs', $specs);
		return $contentBean;
	}
}
